package com.example.springbootwebsocke.pojo;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class MessaheUtils {
    //封装消息的工具类  后端传递给前端的消息
    public static String getMessage(boolean isSysteMessge,String fromNamem,Object message){
      try{
          ResultMessage resultMessage=new ResultMessage();// 服务器发送到浏览器的websocket数据
          resultMessage.setFromName(fromNamem);
          resultMessage.setMessage(message);
          resultMessage.setSystem(isSysteMessge);
          if (!StringUtils.isEmpty(fromNamem)){
              resultMessage.setFromName(fromNamem);
          }
          ObjectMapper mapper=new ObjectMapper();//弄成json数据
          return mapper.writeValueAsString(resultMessage);
    }catch (Exception e){
          e.printStackTrace();
      }
      return null;
}}
